package com.weixin.paleituo.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.weixin.paleituo.R;
import com.weixin.paleituo.help.Holder;
import com.weixin.paleituo.help.HolderCreator;
import com.weixin.paleituo.view.LoopViewPager;

import java.util.List;

/**
 * Author yichao
 * Time  2018/3/15 15:01
 * Dest  ${TODO}
 */

public class GoodsHomePagerAdapter<T> extends PagerAdapter {
    private Context mContext;
    private List<String> mStringLsit;
    private boolean canLoop = true;
    private LoopViewPager viewPager;
    private final int MULTIPLE_COUNT = 300;
    private HolderCreator holderCreator;


    public int toRealPosition(int position) {
        int realCount = getRealCount();
        if (realCount == 0)
            return 0;
        int realPosition = position % realCount;
        return realPosition;
    }
    @Override
    public int getCount() {
        return canLoop ? getRealCount()*MULTIPLE_COUNT : getRealCount();
    }

    public int getRealCount() {
        return mStringLsit == null ? 0 : mStringLsit.size();
    }


    public GoodsHomePagerAdapter(Context context, List<String> stringLsit,HolderCreator holderCreator) {
        mContext = context;
        mStringLsit = stringLsit;
        this.holderCreator = holderCreator;
    }


    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }
    @Override
    public void finishUpdate(ViewGroup container) {
        int position = viewPager.getCurrentItem();
        if (position == 0) {
            position = viewPager.getFristItem();
        } else if (position == getCount() - 1) {
            position = viewPager.getLastItem();
        }
        try {
            viewPager.setCurrentItem(position, false);
        }catch (IllegalStateException e){}
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
       /* ImageView imageView=new ImageView(mContext);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setImageResource(R.mipmap.originality_ad);
        container.addView(imageView);

        return imageView;*/
        int realPosition = toRealPosition(position);

        View view = getView(realPosition, null, container);
        //        if(onItemClickListener != null) view.setOnClickListener(onItemClickListener);
        container.addView(view);
        return view;
    }


    public View getView(int position, View view, ViewGroup container) {
        Holder holder = null;
        if (view == null) {
            holder = (Holder) holderCreator.createHolder();
            view = holder.createView(container.getContext());
            view.setTag(com.bigkoo.convenientbanner.R.id.cb_item_tag, holder);
        } else {
            holder = (Holder<T>) view.getTag(R.id.item_tag);
        }
        if (mStringLsit != null && !mStringLsit.isEmpty())
            holder.UpdateUI(container.getContext(), position, mStringLsit.get(position));
        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }
    public void setCanLoop(boolean canLoop) {
        this.canLoop = canLoop;
    }

    public void setViewPager(LoopViewPager viewPager) {
        this.viewPager = viewPager;
    }

}
